package com.caifu.util;

import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.UUID;


@Component
public class NoGenUtil {

    /**
     * 功能描述: 生成随机数字和字母
     *
     * @auther: Lyf
     * @date: 2020/7/8 10:38
     * @param: length  生成位数
     * @return:
     */
    public static String getStringRandom(int length) {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * 功能描述: 获取32位uuid字符串
     * @Author: szt
     * @Date:   2020/10/14
     */
    public static String getUUID_32(){
        return UUID.randomUUID().toString().replace("-","");
    }

    //获取指定长度的一串随机数
    public static String getNumberRandom(int n) {
        StringBuffer buf = new StringBuffer();
        for(int i=0;i<n;i++) {
            buf.append(new Random().nextInt(9));
        }
        return buf.toString();
    }



}
